﻿var setHeightOrWidth = function()
    {
        $(".leftPage").width("268px");
        var h1 = $("#rightPage").height();
        //var h2 = window.screen.height - 245;
        var h2 = $(window).innerHeight() - 132;
        //var h2 = document.documentElement.clientHeight;
        var h = h1 > h2 ? h1 : h2;
        $(".leftPage").height(h);
    }

var showEditZone = function()
    {
        var h = ($(window).height()-$("#editZone").height())/2;
        var w = ($(window).width() - $("#editZone").width())/2;
        showGrayBg();
        document.getElementById("editZone").style.left = w.toString() + "px";
        document.getElementById("editZone").style.top =  h.toString() + "px";
        $("#editZone").show();
        
    }
    
var hideEditZone = function()
    {
        hideGrayBg();
        $("#editZone").hide();
    }   

/*-------------------------------itemOperation Start-------------------------------*/    
var itemOpt = {

test: function()
    {
      alert("dd");
    },
        
addItem: function()
    {
        $(".addItem").click(addItemClick);
        

        function addItemClick()
        {
            showEditZone();
            var ChapterNo = $(this).parent().children("input").val();
            $(".eChapterNo").val(ChapterNo); //将章节号传递到编辑区域
            $(".eItemNo").val(""); //将工作项目号清零
            $(".editZone .itemContent input").val("");
            $(".editZone .faultResultContent .faultResultArea").val("");
            specialSuggestion = $(".editZone .specialSuggestion .specialSuggestionArea").val("");
                       
            var html = '<label for="engineer">工程师参与</label><input id="engineer" type="checkbox" value="engineer" />';
                html += '<label for="skilledPeople">熟练人员参与</label><input id="skilledPeople" type="checkbox" value="skilledPeople" />';
                html += '<label for="qc">互相检查</label><input id="qc" type="checkbox" value="qc" />';
                if($(".editZone .suggestionContent").html() == "")
                {
                    $(html).appendTo($(".editZone .suggestionContent")); 
                }
			//清除checkbox选项
            $(".editZone .suggestionContent input[type=checkbox]").removeAttr("checked");
            if($(".editZone .major").html() == "")
            {
                $.ajax({
                    type: "GET",
                    url: "ajax/ItemManage.aspx?action=getmajor",
                    dataType: "text",
					cache: false,
                    success: function(str){
                                 var str1 = str.split("|");
                                 var marjorHtml = '<select id="majorSelect" name="majorSelect">';
                                 for(var i=1; i< str1.length; i++)
                                 {
                                    marjorHtml += '<option>';  
                                    marjorHtml += str1[i];
                                    marjorHtml += '</option>';
                                 } 
                                 marjorHtml += '</select>';
                                 $(marjorHtml).appendTo($(".editZone .major"));                         
                             }                  
                  });
            }       
        }
    },

submitItem: function()
    {
        $(".editZone .submit").click(submitItemClick);
        function submitItemClick()
        {
            var item = $(".editZone .itemContent input").val(); //获取项目名称
            var ChapterNo = $(".eChapterNo").val(); //获取章节号
            var itemNo = $(".eItemNo").val(); //获取工作项目编号
            var major = $(".editZone #majorSelect").val(); //获取专业
            var faultResult = $(".editZone .faultResultContent .faultResultArea").val();
            var suggestion = $(".editZone .suggestionContent input[type=checkbox]:checked").val([]);
            var suggest = new Array();
            for(var i=0; i<suggestion.length; i++)
            {
               suggest[i] = suggestion[i].value;
            }
            var specialSuggestion = $(".editZone .specialSuggestion .specialSuggestionArea").val();
            if(item != "" && faultResult != "")
            {
                if(itemNo == "" && ChapterNo != "")
                {
                    $.ajax({
                            type: "GET",
                            contentType: "application/x-www-form-urlencoded; charset=utf-8",
                            url: "ajax/ItemManage.aspx?action=insert",
							cache: false,
                            dataType: "text",
                            data: "ChapterNo=" + ChapterNo + "&itemNo=" + itemNo + "&item=" + encodeURI(item) +  "&major=" +  major + "&faultResult=" + encodeURI(faultResult) +  "&specialSuggestion=" + encodeURI(specialSuggestion) + "&suggestion=" + suggest.join('|'),
                            success: function(msg){window.location.reload();}
                          });
                }
                else if(itemNo != "")
                {
                    $.ajax({
                            type: "GET",
                            contentType: "application/x-www-form-urlencoded; charset=utf-8",
                            url: "ajax/ItemManage.aspx?action=update",
                            dataType: "text",
							cache: false,
                            data: "ChapterNo=" + ChapterNo + "&itemNo=" + itemNo + "&item=" + encodeURI(item) +  "&major=" +  major + "&faultResult=" + encodeURI(faultResult) +  "&specialSuggestion=" + encodeURI(specialSuggestion) + "&suggestion=" + suggest.join('|'),
                            success: function(msg){
                            //alert(msg);
                            window.location.reload();}
                          });
                }
            }
            else
            {
                alert("项目名称不能为空！");
            }
        }
    },

DeleteItem: function(){

    $(".itemOuter .delete").click(DeleteClick);
    
    function DeleteClick()
    {
        if(confirm("确定要删除吗?"))
        {
        var ItemNo = $(this).parent().parent().children(".item").children(".itemNo").val();
        $.ajax({
                type: "GET",
				cache: false,
                url: "ajax/ItemManage.aspx?action=delete",
                data: "itemNo=" + ItemNo,
                success: function(msg){
                                    window.location.reload();
                                    alert(msg);
                                 }
              });   
       }     
    }
},

ModifyItem: function()
    {
        $(".itemOuter .modify").click(ModifyClick);
        
        function ModifyClick()
        {   
            var itemNo = $(this).parent().parent().children(".item").children(".itemNo").val();            
            $.ajax({
                      type: "GET",
                      dataType: "xml",
                      data: "itemNo=" + itemNo,
					  cache: false,
                      url: "ajax/ItemManage.aspx?action=modify",
                      success: function(msg)
                      {
                            //alert($(msg).find("Item").text());
                            var ChapterNo = $(msg).find("ChapterNo").text();
                            var ItemNo = $(msg).find("ItemNo").text();
                            var Item = $(msg).find("Item").text();
                            var Major = $(msg).find("Major").text();
                            var FaultResult = $(msg).find("Fault_Result").text();
                            var isEngineer = $(msg).find("Engineer").text();
                            var isSkilledPeople = $(msg).find("SkilledPeople").text();
                            var isQC = $(msg).find("QC").text();
                            var Measures = $(msg).find("Measures").text();
                            
                            showEditZone();//显示编辑窗
                            
                            $(".eChapterNo").val(ChapterNo); //将章节号传递到编辑区域
                            $(".eItemNo").val(ItemNo); //工作项目号
                            $(".editZone .itemContent input").val(decodeURI(Item));
                            $(".editZone .faultResultContent .faultResultArea").val(decodeURI(FaultResult));
                            $(".editZone .specialSuggestion .specialSuggestionArea").val(decodeURI(Measures));
                            //checkbox
                            var html = '<label for="engineer">工程师参与</label><input id="engineer" type="checkbox" value="engineer" />';
                            html += '<label for="skilledPeople">熟练人员参与</label><input id="skilledPeople" type="checkbox" value="skilledPeople" />';
                            html += '<label for="qc">互相检查</label><input id="qc" type="checkbox" value="qc" />';
                            if($(".editZone .suggestionContent").html() == "")
                            {
                                $(html).appendTo($(".editZone .suggestionContent")); 
                            }
                            //清除checkbox选项
                            $(".editZone .suggestionContent input[type=checkbox]").removeAttr("checked");
                            
                            if(isEngineer == "true")
                            {
                                $("#engineer").attr("checked","checked");
                            }
                            if(isSkilledPeople == "true")
                            {
                                $("#skilledPeople").attr("checked","checked");
                            }
                            if(isQC == "true")
                            {   
                                $("#qc").attr("checked","checked");
                            }
                            
                            //select option
                            if($(".editZone .major").html() == "")
                            {
                                $.ajax({
                                        type: "GET",
                                        url: "ajax/ItemManage.aspx?action=getmajor",
                                        dataType: "text",
										cache: false,
                                        success: function(str)
                                        {
                                             var str1 = str.split("|");
                                             var marjorHtml = '<select id="majorSelect" name="majorSelect">';
                                             for(var i=1; i< str1.length; i++)
                                             {
                                                marjorHtml = marjorHtml + '<option value="' + str1[i] + '">';  
                                                marjorHtml += str1[i];
                                                marjorHtml += '</option>';
                                             } 
                                             marjorHtml += '</select>';
                                             $(marjorHtml).appendTo($(".editZone .major"));                         
                                        }                  
                                     });
                            } 
                            $(".editZone #majorSelect option").removeAttr("selected");
                            $("#majorSelect").val(Major);      
                      }              
              });//加载工作项目内容ajax结束
        }
    }, 
        
Cancel: function()
    {
        $(".cancel").click(cancelClick); 
        function cancelClick()
        {
            hideEditZone();
            $(".eChapterNo, .eItemNo").val("");
        }         
                  
    }
}
 
/*-------------------------------itemOperation End-------------------------------*/  
    
var riskItemManage = function()
    {
        setHeightOrWidth();
        itemOpt.addItem();
        itemOpt.submitItem();
        itemOpt.Cancel();
        itemOpt.DeleteItem();
        itemOpt.ModifyItem();
        
    } 
    
/*-----------------------------------EditZone Start-----------------------------------*/
document.writeln('<style>');
document.writeln('.editZone{display:none; z-index:10; background:#fff; position: absolute;}');    
document.writeln('.editZone .clear{clear:both;}');
document.writeln('.editZone{width: 620px; padding-left:5px; border:4px solid #CCC;}');    
document.writeln('.editZone div{/*border:1px dotted #adf;*/}');
document.writeln('.editZone input{width:400px;}');    
document.writeln('.editZone textarea{width:600px; height:130px;}');
document.writeln('.editZone .title{height:26px; line-height:26px;}');  
document.writeln('.editZone .itemContent{position:relative;}');  
document.writeln('.editZone .itemContent .major{position:absolute; left:540px; top:0;}');
document.writeln('.editZone .suggestionContent{height:14px; width:600px; border: 1px dotted #ccc; padding-top:4px; padding-bottom:4px; font-size: 14px;}');
document.writeln('.editZone .suggestionContent label{display:block; float:left; margin-left:20px;}');
document.writeln('.editZone .suggestionContent input{display:block; float:left; width:20px;}');
document.writeln('.editZone .manage{padding-top:5px; padding-bottom:2px;}');    
document.writeln('.editZone .manage span{display:block; float:right; padding-right:5px; height:26px; line-height:26px; background:#99D6FF; width:60px; text-align:right; margin-right: 25px;}');

document.writeln('.editZone .itemOuter .submit{background-image:url("Images/submit.png"); background-repeat:no-repeat; background-position:5px center;}');
document.writeln('.editZone .itemOuter .cancel{background-image:url("Images/delete.png"); background-repeat:no-repeat; background-position:5px center;}');

document.writeln('</style> ');    
document.writeln('<div class="editZone" id="editZone">');    
document.writeln('<input class="eChapterNo" type="hidden" value="" /> <input class="eItemNo" type="hidden" value="" />');
document.writeln('<div class="itemOuter">');    
document.writeln('<div class="item"><div class="itemTitle title">工作项目</div><div class="itemContent content">');
document.writeln('<input type="text" name="itemName" value="" /><span class="major"></span>');    
document.writeln('</div></div>');
document.writeln('<div class="faultResult"><div class="faultResultTitle title">可能的差错及后果</div><div class="faultResultContent content"><textarea class="faultResultArea"></textarea></div></div>');    
document.writeln('<div class="suggestion"><div class="suggestionTitle title">建议采取的措施</div><div class="suggestionContent content"></div></div>');
document.writeln('<div class="specialSuggestion"><div class="specialSuggestionTitle title">专项措施</div><div class="specialSuggestionContent content"><textarea class="specialSuggestionArea"></textarea></div></div>');    
document.writeln('<div class="manage"><span class="button cancel">取消</span><span class="button submit">提交</span><div class="clear"></div></div>');
document.writeln('</div></div>');
/*-----------------------------------EditZone End-----------------------------------*/